#include "CGamePlayState.h"
#include "CGameClass.h"
//#include "TextureManager.h"
#include <fstream>
#include <string>
#include "CShape.h"
#include "CPlayer.h"
#include "CRender.h"
#include "CSGD_FModManager.h"
#include "CSGD_DirectInput.h"
#include "CCloud.h"
#include <iostream>
using std::cout;
#include "testenum.h"
#include "CAnimationEngine.h"
#include "CCamera.H"

#include "CPlatform.h"
#include "CSunshine.h"

#include "CChangeBackground.h"

#include "CGWindow.h"
CGamePlayState CGamePlayState::Instance;

void CGamePlayState::Enter() {

	CAnimationEngine::GetInstance()->LoadAllAnimations();
	//LoadPicturesFromFile("pictures2load.txt");

	CPlayer *pPlayer = new CPlayer;
	m_Objects.Add(pPlayer);
	pPlayer->Release();

	CChangeBackground::GetInstance()->LoadAllBackGround();


	CCloud *pCloud = new CCloud;
	pCloud->m_pPlayer = pPlayer;
	pPlayer->AddRef();
	pPlayer->m_pCloud = pCloud;
	m_Objects.Add(pCloud);
	pCloud->Release();

	CSunshine *pSunshine= new CSunshine(pCloud, Vec3(0.0f, 17.0f, 0.0f));
	pCloud->AddRef();
	m_Objects.Add(pSunshine);
	pSunshine->Release();
	Vec3 Up(0, 47, 0);
	for (int i = 0; i < 40; i++)
	{
		pSunshine = new CSunshine(pCloud, Up);
		pCloud->AddRef();
		m_Objects.Add(pSunshine);
		pSunshine->Release();

		Up = Up * Mat4::GetRotateZ(i / 40.0f * 3.14159f);
	}

	CGWindow *pWindow;
	int i = 0;
	for (float f = 0.0f; f <= 3.14159f * 2; f += 3.14159f / 11 * 2)
	{
		pWindow = new CGWindow(i, f, f + 3.14159f / 11, 49, 60, 4);
		m_Objects.Add(pWindow);
		pWindow->Release();
		i++;
		if (i == 3)
			i = 0;
	}

	m_pDI = CSGD_DirectInput::GetInstance();
	m_pFMOD = CSGD_FModManager::GetInstance();

	m_GlobeQuad.MakeShape(Vec2(-10, -10), Vec2(10, 10), 1);
	m_pGlobeTexture = CTextureManager::GetInstance()->LoadTexture("img/globe.tga");
	CRainDrop::s_pTexture = CTextureManager::GetInstance()->LoadTexture("img/drop.tga");
	m_pCrustTexture = CTextureManager::GetInstance()->LoadTexture("img/crust.tga");
	m_CrustQuad1.MakeShape(Vec2(-30, -30), Vec2(30, 30), 1);
	m_CrustQuad2.MakeShape(Vec2(-50, -50), Vec2(50, 50), 1);

	
#if LOADSFX
	LoadSFX("audio2load.txt");
#endif LOADSFX
	LoadLevel();
}
bool CGamePlayState::LoadSFX(const char* filename) {
	std::fstream fin(filename, std::ios_base::in );

	if( !fin.is_open() )
	{
		MessageBox( 0, "ERROR LOADING SFX", "ERROR LOADING SFX", 0 );
		return false;
	}

	// keep loading pictures till end of file
	std::string pic_filename;
	std::string endline;

	int mode = 0;

	for( int i = 0; i < NUM_SFX; i++) {
		getline( fin, pic_filename, ' ');
		cout << "Loading " << pic_filename << '\n';
		fin >> mode;
		getline( fin, endline, '\n' );

		// looping?
		(mode) ?
			m_nSFX[i] = m_pFMOD->LoadSound( pic_filename.c_str(), SGD_FMOD_LOOPING ) 
			:
			m_nSFX[i] = m_pFMOD->LoadSound( pic_filename.c_str() );
	}

	fin.close();
	fin.clear();

	// 50%
	m_fVolume = .5f;
	// 30%
	m_fJumpVolume = .3f;
	// 40%
	m_fRainVolume = .4f;

	m_pFMOD->PlaySound( m_nSFX[ BG1_SFX ] );
	m_pFMOD->SetVolume( m_nSFX[ BG1_SFX ], m_fVolume );

	m_pFMOD->PlaySound( m_nSFX[ RAIN_SFX ] );
	m_pFMOD->SetVolume( m_nSFX[ RAIN_SFX ], m_fRainVolume );

	return true;

}
bool CGamePlayState::LoadPicturesFromFile(const char* filename) {
	//std::fstream fin(filename, std::ios_base::in );

	//if( !fin.is_open() )
	//{
	//	MessageBox( 0, "ERROR LOADING PICTURES", "ERROR LOADING PICTURES", 0 );
	//	return false;
	//}

	//// keep loading pictures till end of file
	//std::string pic_filename;

	//getline( fin, pic_filename );
	//m_nTextureIDs[TEX_PLAYER] = m_pTM->LoadTexture( pic_filename.c_str() );
	//getline( fin, pic_filename );
	//m_nTextureIDs[TEX_CLOUD] = m_pTM->LoadTexture( pic_filename.c_str() );

	//fin.close();
	//fin.clear();

	return true;
}

void CGamePlayState::Input() {
	static int currentTrack = 1;

#if LOADSFX
	if( m_pDI->KeyPressed( DIK_P ) )
	{
		//m_pFMOD->StopSound( m_nSFX[ currentTrack] );
		currentTrack++;
		if( currentTrack >= NUM_SFX )
			currentTrack = 1;
		m_pFMOD->PlaySound( m_nSFX[ currentTrack] );
		m_pFMOD->SetVolume( m_nSFX[ currentTrack], m_fVolume );
	}
	if( m_pDI->KeyPressed( DIK_O ) )
	{
		//m_pFMOD->StopSound( m_nSFX[ currentTrack] );
		currentTrack--;
		if( currentTrack < 1 )
			currentTrack = NUM_SFX-1;
		m_pFMOD->PlaySound( m_nSFX[ currentTrack] );
		m_pFMOD->SetVolume( m_nSFX[ currentTrack], m_fVolume );
	}
#endif LOADSFX
}

void CGamePlayState::Update(float dt) {
	if (m_pDI->KeyDown(DIK_HOME))
	{
		CCamera::GetInstance()->m_CamMat.waxis.z -= 10;
	}
	else if (m_pDI->KeyDown(DIK_END))
	{
		CCamera::GetInstance()->m_CamMat.waxis.z += 10;
		//CCamera::GetInstance()->TranslateLocalZ(100);
	}
	m_Objects.CheckCollisions();
	m_Objects.UpdateAll(dt);
	
	CChangeBackground::GetInstance()->Transition(dt);
}

void CGamePlayState::Render() {
	//m_devControl->SetViewMatrix(m_Camera.GetViewMatrix());
	//m_devControl->SetProjectionMatrix(m_Camera.GetProjectionMatrix());
CChangeBackground::GetInstance()->Render();
	CRender::GetInstance()->DrawTex(m_pGlobeTexture, &m_GlobeQuad, Vec2(0, 0), -1);
	CRender::GetInstance()->DrawTex(m_pCrustTexture, &m_CrustQuad1, Vec2(0, 0), -1);
	CRender::GetInstance()->DrawTex(m_pCrustTexture, &m_CrustQuad2, Vec2(0, 0), -1);

	m_Objects.RenderAll();

	/*
	glEnable(GL_TEXTURE_2D);
	//glColor3f(1, 1, 1);
	glBindTexture(GL_TEXTURE_2D, 1);
	glPushMatrix();
	glTranslatef(0, 0, -80);
	glEnableClientState(GL_VERTEX_ARRAY);
	glEnableClientState( GL_TEXTURE_COORD_ARRAY);
	//glBindBuffer(GL_ARRAY_BUFFER, newShape.m_bufferHandles[0]);
	glVertexPointer(2, GL_FLOAT, 0, newShape.GetVertexDataPointer());
	glTexCoordPointer(2, GL_FLOAT, 0, newShape.GetTextureCoordPointer());
	glDrawArrays(GL_TRIANGLE_STRIP, 0, newShape.m_numberVerts);
	glDisable(GL_TEXTURE_2D);
<<<<<<< .mine
	glPopMatrix();
=======
	//glPopMatrix();*/
}

void CGamePlayState::Exit() 
{
	//SAFE_RELEASE( m_effect );
#if LOADSFX
	for(int i = 0; i < NUM_SFX; i++)
		m_pFMOD->UnloadSound( m_nSFX[i] );
#endif LOADSFX

	CTextureManager::GetInstance()->ReleaseAll();
	m_Objects.Clear();
	//m_pTM->ReleaseAll();
}
//
//void CGamePlayState::OnLostDevice(void) {
//	m_Objects.OnLostDevice();
//}